package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.biz.ProCustomRecordBiz;
import com.mall.product.domain.biz.ProProductBiz;
import com.mall.product.domain.entity.ProCustomRecord;
import com.mall.product.domain.entity.ProProduct;
import com.mall.product.domain.req.ProProductRequest;
import com.mall.product.service.ProBrandService;
import com.mall.product.service.ProCustomRecordService;
import com.mall.product.service.ProProductService;
import com.mall.product.service.ProTypeService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品Controller
 *
 * @author zhonglin
 * @date 2020-05-07
 */
@Controller
@RequestMapping("/product/product")
public class ProProductController
    extends BaseController {

  private String prefix = "product/product";

  @Autowired
  private ProProductService      proProductService;
  @Autowired
  private ProBrandService        proBrandService;
  @Autowired
  private ProTypeService         proTypeService;
  @Autowired
  private ProCustomRecordService proCustomRecordService;

  @RequiresPermissions("product:product:view")
  @GetMapping()
  public String product(ModelMap mmap) {

    mmap.put("brands", proBrandService.selectBrandAll());
    mmap.put("types", proTypeService.selectProTypeAll());

    return prefix + "/product";
  }

  /**
   * 查询商品列表
   */
  @RequiresPermissions("product:product:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProProduct proProduct) {

    startPage();
    List<ProProductBiz> list = proProductService.selectProProductList(proProduct);
    return getDataTable(list);
  }

  /**
   * 导出商品列表
   */
  @RequiresPermissions("product:product:export")
  @Log(title = "商品", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProProduct proProduct) {

    List<ProProductBiz>      list = proProductService.selectProProductList(proProduct);
    ExcelUtil<ProProductBiz> util = new ExcelUtil<ProProductBiz>(ProProductBiz.class);
    return util.exportExcel(list, "product");
  }

  /**
   * 新增商品
   */
  @GetMapping("/add")
  public String add(ModelMap mmap) {

    mmap.put("brands", proBrandService.selectBrandAll());
    mmap.put("types", proTypeService.selectProTypeAll());

    return prefix + "/add";
  }

  /**
   * 新增保存商品
   */
  @RequiresPermissions("product:product:add")
  @Log(title = "商品", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProProductRequest productRequest) {

    return toAjax(proProductService.insertProProduct(productRequest));
  }

  /**
   * 修改商品
   */
  @GetMapping("/edit/{productId}")
  public String edit(@PathVariable("productId") Long productId,
                     ModelMap mmap) {

    ProProductBiz proProduct = proProductService.selectProProductById(productId);
    mmap.put("brands", proBrandService.selectBrandAll());
    mmap.put("product", proProduct);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品
   */
  @RequiresPermissions("product:product:edit")
  @Log(title = "商品", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProProduct proProduct) {

    return toAjax(proProductService.updateProProduct(proProduct));
  }

  /**
   * 删除商品
   */
  @RequiresPermissions("product:product:remove")
  @Log(title = "商品", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proProductService.deleteProProductByIds(ids));
  }

  /**
   * 生成SKU
   */
  @GetMapping("/genSku/{productId}")
  public String genSku(@PathVariable("productId") Long productId,
                       ModelMap mmap) {

    ProProductBiz proProduct = proProductService.selectProProductById(productId);

    ProCustomRecord proCustomRecord = new ProCustomRecord();
    proCustomRecord.setTypeId(proProduct.getTypeId());
    proCustomRecord.setType("0");//属性
    List<ProCustomRecordBiz> customRecords = proCustomRecordService.selectProCustomRecordList(proCustomRecord);

    mmap.put("product", proProduct);
    mmap.put("customRecords", customRecords);
    return prefix + "/genSku";
  }

  /**
   * 生成SKU
   */
  @GetMapping("/image/detail/{productId}")
  public String imageDetail(@PathVariable("productId") Long productId,
                       ModelMap mmap) {

    ProProductBiz proProduct = proProductService.selectProProductById(productId);
    mmap.put("product", proProduct);
    return "product/image/image";
  }

}
